Python - Format Strings

சர வடிவமைத்தல் - நவீன முறைகள்

Python - சர வடிவமைத்தல்

பைத்தான் மாறிகள் அத்தியாயத்தில் நாம் கற்றுக்கொண்டது போல, சரங்களையும் எண்களையும் இவ்வாறு இணைக்க முடியாது:

Error Example

age = 36
# This will produce an error:
txt = "My name is John, I am " + age
print(txt)
# TypeError: can only concatenate str to str

ஆனால் f-strings அல்லது format() முறையைப் பயன்படுத்தி சரங்களையும் எண்களையும் இணைக்கலாம்!

F-Strings

F-String என்ன?

F-String பைத்தான் 3.6 இல் அறிமுகப்படுத்தப்பட்டது, மேலும் இப்போது சரங்களை வடிவமைக்க விரும்பப்படும் வழியாகும்.

ஒரு சரத்தை f-string ஆக குறிப்பிட, சர மாறிலிக்கு முன்னால் f ஐ வைத்து, மாறிகள் மற்றும் பிற செயல்பாடுகளுக்கான இடங்களாக சுருள் பிரேஸ்களை {} சேர்க்கவும்.

F-String வடிவமைப்பு

f
"My name is John, I am
{age}
"
My name is John, I am 36

age = 36 என்ற மாறி {} இடத்தில் செருகப்படுகிறது

F-String Example

# Create an f-string
age = 36
txt = f"My name is John, I am {age}"
print(txt)  # Output: My name is John, I am 36

இடத்தக்கங்கள் மற்றும் மாற்றிகள்

ஒரு இடத்தக்கத்தில் மாறிகள், செயல்பாடுகள், சார்புகள் மற்றும் மதிப்பை வடிவமைக்க மாற்றிகள் இருக்கலாம்.

அடிப்படை இடத்தக்கங்கள்

Basic Placeholder Example

# Add a placeholder for the price variable
price = 59
txt = f"The price is {price} dollars"
print(txt)  # Output: The price is 59 dollars

மாற்றிகளுடன் இடத்தக்கங்கள்

ஒரு மாற்றி சேர்க்கப்பட்டுள்ளது : அதைத் தொடர்ந்து ஒரு சட்டப்பூர்வ வடிவமைப்பு வகையைச் சேர்ப்பதன் மூலம், .2f என்பது 2 தசமங்களைக் கொண்ட நிலையான புள்ளி எண்ணைக் குறிக்கிறது:

மாற்றி வடிவமைப்பு

f"The price is
{price:.2f}
dollars"
The price is 59.00 dollars

.2f மாற்றி 2 தசம இடங்களுக்கு வடிவமைக்கிறது

Format Modifier Example

# Display the price with 2 decimals
price = 59
txt = f"The price is {price:.2f} dollars"
print(txt)  # Output: The price is 59.00 dollars

இடத்தக்கங்களில் செயல்பாடுகள்

ஒரு இடத்தக்கத்தில் பைத்தான் குறியீடு இருக்கலாம், கணித செயல்பாடுகள் போன்றவை:

Operations in Placeholders Example

# Perform a math operation in the placeholder
txt = f"The price is {20 * 59} dollars"
print(txt)  # Output: The price is 1180 dollars

# Complex expressions
a = 10
b = 20
txt = f"The sum is {a + b} and product is {a * b}"
print(txt)  # Output: The sum is 30 and product is 200

# Function calls
name = "john"
txt = f"Name in uppercase: {name.upper()}"
print(txt)  # Output: Name in uppercase: JOHN

# Multiple operations
items = 5
price_per_item = 12.5
txt = f"Total: ${items * price_per_item:.2f}"
print(txt)  # Output: Total: $62.50

💡 F-String சக்தி:

F-strings ஒரு இடத்தக்கத்தின் உள்ளே எந்தவொரு செல்லுபடியாகும் பைத்தான் வெளிப்பாடையும் செயல்படுத்த அனுமதிக்கின்றன, இது அவற்றை மிகவும் சக்திவாய்ந்ததாக்குகிறது.

பொதுவான வடிவமைப்பு மாற்றிகள்

மாற்றி விளக்கம் எடுத்துக்காட்டு வெளியீடு
:.2f 2 தசம இடங்களுடன் மிதவை எண் f"{3.14159:.2f}" 3.14
:.0f தசமங்கள் இல்லாமல் மிதவை எண் f"{3.14159:.0f}" 3
:d முழு எண் வடிவமைப்பு f"{42:d}" 42
:x பதினறும எண் (சிறிய எழுத்துகள்) f"{255:x}" ff
:X பதினறும எண் (பெரிய எழுத்துகள்) f"{255:X}" FF
:b இரும எண் f"{5:b}" 101
:o அஷ்ட எண் f"{8:o}" 10
:, ஆயிரம் பிரிப்பான் f"{1000000:,}" 1,000,000
:<10 இடது சீரமைப்பு (10 இடங்கள்) f"{'Hi':<10}" Hi        
:>10 வலது சீரமைப்பு (10 இடங்கள்) f"{'Hi':>10}"         Hi
:^10 மைய சீரமைப்பு (10 இடங்கள்) f"{'Hi':^10}"     Hi    
:% சதவீத வடிவம் f"{0.25:%}" 25.000000%

பயிற்சி - F-String வடிவமைத்தல்

x = 9 எனில், 'The price is 9.00 dollars' என அச்சிடுவதற்கான சரியான தொடரியல் எது?

சரியான தொடரியலைத் தேர்ந்தெடுக்கவும்:

print(f'The price is {x:.2f} dollars')
✓ சரி! .2f மாற்றி 2 தசம இடங்களுடன் மிதவை எண்ணாக வடிவமைக்கிறது
print(f'The price is {x:2} dollars')
✗ தவறு! :2 என்பது சீரமைப்புக்கானது, தசம இடங்களுக்கு அல்ல
print(f'The price is {x:format(2)} dollars')
✗ தவறு! இது செல்லுபடியாகும் தொடரியல் அல்ல. F-strings இல் format() முறையைப் பயன்படுத்த முடியாது

விளக்கம்:

f'The price is
{x:.2f}
dollars'
The price is 9.00 dollars

.2f மாற்றி x மதிப்பை 2 தசம இடங்களுடன் வடிவமைக்கிறது

format() முறை

F-strings க்கு முன், format() முறை பரவலாகப் பயன்படுத்தப்பட்டது:

format() முறை

# Using format() method
name = "John"
age = 36
txt = "My name is {}, I am {}".format(name, age)
print(txt)  # Output: My name is John, I am 36

# With index positions
txt = "My name is {0}, I am {1}".format(name, age)
print(txt)  # Output: My name is John, I am 36

# With named parameters
txt = "My name is {n}, I am {a}".format(n=name, a=age)
print(txt)  # Output: My name is John, I am 36

F-String (நவீன முறை)

# Using f-string (modern way)
name = "John"
age = 36
txt = f"My name is {name}, I am {age}"
print(txt)  # Output: My name is John, I am 36

# With expressions
txt = f"My name is {name.upper()}, I am {age + 5}"
print(txt)  # Output: My name is JOHN, I am 41

🔄 F-String vs format():

F-strings வாசிக்க எளிதானவை, எழுத எளிதானவை மற்றும் format() முறையை விட வேகமானவை. புதிய குறியீட்டுக்கு எப்போதும் f-strings பயன்படுத்தவும்.

மேம்பட்ட வடிவமைத்தல்

கூட்டு மாற்றிகள்

# Combine multiple modifiers
number = 1234567.8912

# Currency with 2 decimals and thousand separators
print(f"${number:,.2f}")  # Output: $1,234,567.89

# Percentage with 1 decimal
percentage = 0.856
print(f"{percentage:.1%}")  # Output: 85.6%

# Scientific notation
print(f"{number:.2e}")  # Output: 1.23e+06

# Different width and alignment
name = "Python"
print(f"|{name:<20}|")  # Output: |Python              |
print(f"|{name:>20}|")  # Output: |              Python|
print(f"|{name:^20}|")  # Output: |       Python       |

நிபந்தனை வெளிப்பாடுகள்

# Conditional expressions in f-strings
score = 85
grade = f"Score: {score} - {'Pass' if score >= 50 else 'Fail'}"
print(grade)  # Output: Score: 85 - Pass

# Multiple conditions
temperature = 22
feeling = f"It feels {'hot' if temperature > 30 else 'warm' if temperature > 20 else 'cool'}"
print(feeling)  # Output: It feels warm

# Formatting based on value
value = 42
formatted = f"Value: {value:0>5}"  # Pad with zeros to width 5
print(formatted)  # Output: Value: 00042

நடைமுறை எடுத்துக்காட்டுகள்

விலைப்பட்டியல்

item = "Python Book"
price = 29.99
quantity = 3
total = price * quantity
invoice = f"""
Item: {item}
Price: ${price:.2f}
Quantity: {quantity}
Total: ${total:.2f}
"""

தரவு அறிக்கை

users = 1500
growth = 0.25
report = f"""
Total Users: {users:,}
Growth Rate: {growth:.1%}
Projected: {int(users * (1 + growth)):,}
"""

பயனர் சுயவிவரம்

name = "John Doe"
age = 30
salary = 75000.50
profile = f"""
Name: {name:>15}
Age: {age:>16}
Salary: ${salary:>12,.2f}
"""

டீபக் செய்தி

variable = "test"
value = 42.123456
debug = f"[DEBUG] {variable}={value:.3f}"
# [DEBUG] test=42.123

பழைய சர வடிவமைத்தல் முறைகள்

முறை எடுத்துக்காட்டு விளக்கம் பரிந்துரை
% ஆபரேட்டர் "Name: %s, Age: %d" % ("John", 30) மிகவும் பழைய முறை, C இல் இருந்து கடன் தவிர்க்கவும்
str.format() "Name: {}, Age: {}".format("John", 30) Python 2.6+ பழைய குறியீட்டுக்கு
f-string f"Name: {'John'}, Age: {30}" Python 3.6+ பரிந்துரைக்கப்படுகிறது

⚠️ சர வடிவமைத்தல் பற்றி மேலும் அறிய:

சர வடிவமைத்தல் பற்றி மேலும் அறிய எங்கள் String Formatting அத்தியாயத்தைப் பார்க்கவும்.